#include "mcadincl.h"
#include <string.h>

#define  INTERRUPTED            1
#define  INSUFFICIENT_MEMORY    2
#define  MUST_BE_REAL           3
#define  NUMBER_OF_ERRORS       3   

    // table of error messages
    // if your function never returns an
    // error -- you do not need to create this
    // table
    char * myErrorMessageTable[NUMBER_OF_ERRORS] =  
    {   "interrupted",
        "insufficient memory",
        "must be real"
    };
    
    
    // this code executes the multiplication
    // see the information of MathcadUserFunction
    // to find out more
    LRESULT  MultiplyRealArrayByRealScalar(  //COMPLEXSCALAR*const Ret, 
	                                          LPMCSTRING Ret,
															//COMPLEXARRAY * const Product,
        const COMPLEXSCALAR * const Scalar, const COMPLEXARRAY * const Array,
															LPCMCSTRING Str )
    {
        unsigned int row, col;

        // check that the array argument is real
        if ( Array->hImag != NULL )
            // if it is not, display "must be real" error message 
            // under the array argument( the 2nd argument )
            return MAKELRESULT(MUST_BE_REAL,2);
            
        // allocate memory for the result.
		  Ret->str = MathcadAllocate( strlen(Str->str) + 3 + 1 );
		  strcat( Ret -> str, "abc" );
		  strcat( Ret -> str, Str->str );
        // normal return
        return 0;
    }

    // fill out a FUNCTIONINFO structure with
    // the information needed for registering the
    // function with Mathcad
    FUNCTIONINFO multiply = 
    {
    // Name by which mathcad will recognize the function
    "multiply",        
    
    // description of "multiply" parameters to be used
    // by the Insert Function dialog box
    "a,M,s",   
    
    // description of the function for the Insert Function dialog box       
    "returns the product of real scalar a and real array M",    
    
    // pointer to the executible code
    // i.e. code that should be executed
    // when a user types in "multiply(a,M)="
    (LPCFUNCTION)MultiplyRealArrayByRealScalar,  
        
    // multiply(a,M) returns a complex array
	 //	 COMPLEX_SCALAR,
	 	 STRING,
    //COMPLEX_ARRAY,
        
    // multiply(a,M) takes on two arguments
    3,   
    
    // the first is a complex scalar, the second a complex array 
    { COMPLEX_SCALAR, COMPLEX_ARRAY, STRING} 
    };
    

    // DLL entry point code
    // the _CRT_INIT function is needed
    // if you are using Microsoft's 32 bit compiler
 
    BOOL WINAPI _CRT_INIT(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved);

    BOOL WINAPI  DllEntryPoint (HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved)
    {
        switch (dwReason)
        {
                case DLL_PROCESS_ATTACH:

                //
                // DLL is attaching to the address space of 
                // the current process.
                //
                    if (!_CRT_INIT(hDLL, dwReason, lpReserved)) 
                        return FALSE;
                
                
                    // register the error message table
                    // Note, that if your function never returns
                    // an error -- you do not need to 
                    // register an error message table
                    if ( CreateUserErrorMessageTable(
                            hDLL, NUMBER_OF_ERRORS, myErrorMessageTable ) )
                        // and if the errors register OK
                        // go ahead and
                        // register user function
                        CreateUserFunction( hDLL, &multiply );
                    break;


                case DLL_THREAD_ATTACH:
                case DLL_THREAD_DETACH:
                case DLL_PROCESS_DETACH:

                    if (!_CRT_INIT(hDLL, dwReason, lpReserved)) 
                        return FALSE;
                    break;
                    
        }
        return TRUE;
    }

#undef INTERRUPTED    
#undef INSUFFICIENT_MEMORY
#undef MUST_BE_REAL   
#undef NUMBER_OF_ERRORS     

    

